//
//  AddItemCell.m
//  linkingGame
//
//  Created by zw wu on 15/5/10.
//  Copyright (c) 2015年 com.jinWu. All rights reserved.
//

#import "AddItemCell.h"

@interface AddItemCell ()

@property (nonatomic,retain) UIView *myHighlightView;

@end

@implementation AddItemCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.layer.borderWidth = 1;
        self.layer.masksToBounds = YES;
        self.layer.borderColor = [[UIColor grayColor] CGColor];
        self.layer.cornerRadius = 3;
        
        UIView *myHighlightView = [[UIView alloc]initWithFrame:self.bounds];
        myHighlightView.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.3];
        self.myHighlightView = myHighlightView;
        [myHighlightView release];
        
        UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
        [self addGestureRecognizer:recognizer];
        [recognizer release];
        
        UIImageView *showImageView = [[UIImageView alloc]initWithFrame:self.bounds];
        [self addSubview:showImageView];
        self.showImageView = showImageView;
        [showImageView release];
    }
    
    return self;
}

- (void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
    if (highlighted)
    {
        [self addSubview:self.myHighlightView];
    }
    else
    {
        [self.myHighlightView removeFromSuperview];
    }
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

-(BOOL) canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(goToDelete:))
    {
        return YES;
    }
    
    return NO; //隐藏系统默认的菜单项
}

- (void)longPress:(UILongPressGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan && ![self.pathId isEqualToString:@"AddNewItemViewController_Add"])
    {
        [self becomeFirstResponder];
        
        UIMenuItem *approve = [[UIMenuItem alloc] initWithTitle:@"Delete"action:@selector(goToDelete:)];
        
        UIMenuController *menu = [UIMenuController sharedMenuController];
        [menu setMenuItems:[NSArray arrayWithObjects:approve, nil]];
        [menu setTargetRect:self.bounds inView:self];
        [menu setMenuVisible:YES animated:YES];
    }
}

- (void)goToDelete:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(goToDelete:)])
    {
        [self.delegate goToDelete:self];
    }
}

- (void)dealloc
{
    self.pathId = nil;
    self.myHighlightView = nil;
    self.delegate = nil;
    self.showImageView = nil;

    [super dealloc];
}

@end
